<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.lang.Exception" %>
<%@ page import="com.ykzhujiang.lovespirit.util.PMF" %>
<%@ page import="com.ykzhujiang.lovespirit.util.Utility" %>
<%@ page import="com.ykzhujiang.lovespirit.util.DataManager" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Vector" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="javax.cache.Cache" %>
<%@ page import="javax.cache.CacheException" %>
<%@ page import="javax.cache.CacheFactory" %>
<%@ page import="javax.cache.CacheManager" %>
<%@ page import="com.google.appengine.api.memcache.stdimpl.GCacheFactory" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.Notice" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.LSUser" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.Comment" %>
<%@ page import="com.ykzhujiang.lovespirit.analyzer.Analyzer" %>
<%@ page import="com.ykzhujiang.lovespirit.analyzer.FemaleAnalyzer" %>
<%@ page import="com.ykzhujiang.lovespirit.analyzer.MaleAnalyzer" %>
<%@ page import="com.ykzhujiang.lovespirit.entity.LThread" %>
<%@ page import="java.util.logging.Logger" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<script src="http://www.google.com/jsapi"></script>
<script>
    google.setOnLoadCallback(function(){$(document).ready(pageLoad);});
    google.load("jquery","1.3.2");
    google.load("jqueryui","1.7");
    function pageLoad()
    {
    }
</script>

<%
		Logger logger = Logger.getLogger("comment.jsp");
		int COMMENT_PER_PAGE = 5;
    	UserService userService = UserServiceFactory.getUserService();
    	User user = userService.getCurrentUser();
		String key = request.getParameter("k");
		int start = 0;
		try {
			start = Integer.parseInt(request.getParameter("s"));
		} catch (NumberFormatException e) {
			start = 0;
		}
		logger.info("got key:" + key);
		logger.info("got s:" + start);
    	String query2 = "select from " + Comment.class.getName() + " where sourceUrl == '" + key + 
    		"' order by date desc range " + start +", " + (start + COMMENT_PER_PAGE + 1);
    	List<Comment> comments = null;
    	comments = (List<Comment>)DataManager.getData(query2, false, null);
%>
<%
    if (comments != null && !comments.isEmpty()) {
%>
<dl class="comments-block">
<%    
		Comment c = null;
        for (int i = 0; i < comments.size() && i < COMMENT_PER_PAGE; i++) {
        	c = comments.get(i);
            if (c.getAuthor() == null) {
%>
<dt class="comment-author">匿名人士说:</dt>
<%
            } else {
%>
<dt class="comment-author"><b><%= c.getAuthor().getNickname() %></b> 说:</dt>
<%
            }
%>
<dd class="comment-body"><%= c.getContent() %></dd>
<dd class="comment-footer"><%= c.getDate() %></dd>
<%
        }
%>
</dl>
<%        
    }
%>
	<div class="comments-footer">
<%
	if(comments != null && comments.size() > COMMENT_PER_PAGE) {
%>
		<a id="next-c-<%=key.hashCode()%>" class="next-c" href="javascript:void(0)">下一页</a>
		<script>
			$('#next-c-<%=key.hashCode()%>').click(function(){
				$('#comments-<%=key.hashCode()%>').load('comment.jsp?k=<%=Utility.EncodeUrl(key)%>&s=<%=start+COMMENT_PER_PAGE%>&r='+Math.random());
			});
		</script>
<%
	}
	if(comments != null && start - COMMENT_PER_PAGE >=0) {
%>
		<a id="prev-c-<%=key.hashCode()%>" class="prev-c" href="javascript:void(0)">上一页</a>
		<script>
			$('#prev-c-<%=key.hashCode()%>').click(function(){
				$('#comments-<%=key.hashCode()%>').load('comment.jsp?k=<%=Utility.EncodeUrl(key)%>&s=<%=start-COMMENT_PER_PAGE%>&r'+Math.random());
			});
		</script>
<%
	}
%>
	</div>
